<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width=" ">
      <el-form-item label="用户id" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入用户id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品id" prop="goodsId">
        <el-input v-model="queryParams.goodsId" placeholder="请输入商品id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品数量" prop="goodsCounts">
        <el-input v-model="queryParams.goodsCounts" placeholder="请输入商品数量" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="删除标识" prop="isDel">
        <template slot="label">
          <span>
            删除标识
            <el-tooltip class="item" effect="dark" content="0-正常，1-停用" placement="top">
              <i class="el-icon-question"></i>
            </el-tooltip>
          </span>
        </template>
        <el-input v-model="queryParams.isDel" clearable placeholder="请输入删除标识" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!--      <el-form-item label="扩展字段1" prop="ext1">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.ext1"-->
      <!--          placeholder="请输入扩展字段1"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="扩展字段2" prop="ext2">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.ext2"-->
      <!--          placeholder="请输入扩展字段2"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="扩展字段3" prop="ext3">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.ext3"-->
      <!--          placeholder="请输入扩展字段3"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="扩展字段4" prop="ext4">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.ext4"-->
      <!--          placeholder="请输入扩展字段4"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="扩展字段5" prop="ext5">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.ext5"-->
      <!--          placeholder="请输入扩展字段5"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
    </el-form>

    <el-row :gutter="10" class="mb8" style="display: flex;">
      <el-col style="justify-content: left;" :span="20">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:cart:add']">新增</el-button>
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:cart:edit']">修改</el-button>
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:cart:remove']">删除</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:cart:export']">导出</el-button>
      </el-col>
      <el-col style="justify-content: right;" class="text-right" v-show="showSearch" :span="2.5">
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :span="1.5"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cartList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="购物车id" align="center" prop="cartId" />
      <!-- <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="商品id" align="center" prop="goodsId" /> -->
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="用户真实姓名" align="center" prop="realName" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="商品数量" align="center" prop="goodsCounts" />
      <el-table-column label="商品规格" align="center" prop="stander" />
      <!--      <el-table-column label="扩展字段1" align="center" prop="ext1" />-->
      <!--      <el-table-column label="扩展字段2" align="center" prop="ext2" />-->
      <!--      <el-table-column label="扩展字段3" align="center" prop="ext3" />-->
      <!--      <el-table-column label="扩展字段4" align="center" prop="ext4" />-->
      <!--      <el-table-column label="扩展字段5" align="center" prop="ext5" />-->
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="删除标识" align="center" prop="isDel" />
      <!-- <el-table-column label="更新时间" align="center" prop="updateTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
            v-hasPermi="['monitor:job:query']">详细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改购物车管理对话框 -->
    <el-dialog :title="title" :visible.sync="openEdit" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="购物车id" prop="cartId">
          <el-input v-model="form.cartId" placeholder="购物车id自动生成，不必填写" readonly/>
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="商品id" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入商品id" />
        </el-form-item>
        <el-form-item label="商品数量" prop="goodsCounts">
          <el-input v-model="form.goodsCounts" placeholder="请输入商品数量" />
        </el-form-item>
        <el-form-item label="商品规格" prop="stander">
          <el-input v-model="form.stander" placeholder="请输入商品规格" />
        </el-form-item>
        <el-form-item label="删除标识" prop="isDel">
          <template slot="label">
            <span>
              删除标识
              <el-tooltip class="item" effect="dark" content="0-未删除，1-已删除" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
            </span>
          </template>
          <el-input v-model="form.isDel" placeholder="请输入删除标识"/>
        </el-form-item>
        <!-- <el-form-item label="扩展字段1" prop="ext1">
          <el-input v-model="form.ext1" placeholder="请输入扩展字段1" />
        </el-form-item>
        <el-form-item label="扩展字段2" prop="ext2">
          <el-input v-model="form.ext2" placeholder="请输入扩展字段2" />
        </el-form-item>
        <el-form-item label="扩展字段3" prop="ext3">
          <el-input v-model="form.ext3" placeholder="请输入扩展字段3" />
        </el-form-item>
        <el-form-item label="扩展字段4" prop="ext4">
          <el-input v-model="form.ext4" placeholder="请输入扩展字段4" />
        </el-form-item>
        <el-form-item label="扩展字段5" prop="ext5">
          <el-input v-model="form.ext5" placeholder="请输入扩展字段5" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="openView" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="购物车id" prop="cartId">
          <el-input v-model="form.cartId" placeholder="购物车id自动生成，不必填写" readonly/>
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="未填入用户id" readonly/>
        </el-form-item>
        <el-form-item label="用户真实姓名" prop="realName">
          <template slot="label">
            <span>
              用户真实姓名
              <el-tooltip class="item" effect="dark" content="点击将前往“用户信息管理”修改" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
            </span>
          </template>
          <el-input v-model="form.realName" @click.native="handleClickUser" readonly/>
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickName">
          <template slot="label">
            <span>
              用户昵称
              <el-tooltip class="item" effect="dark" content="点击将前往“用户信息管理”修改" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
            </span>
          </template>
          <el-input v-model="form.nickName" @click.native="handleClickUser" readonly/>
        </el-form-item>
        <el-form-item label="商品id" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="未填入商品id" readonly/>
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsName">
          <template slot="label">
            <span>
              商品名称
              <el-tooltip class="item" effect="dark" content="点击将前往“商品管理”修改" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
            </span>
          </template>
          <el-input v-model="form.goodsName" placeholder="未填入商品名称" @click.native="handleClickGoods" readonly />
        </el-form-item>
        <el-form-item label="商品数量" prop="goodsCounts">
          <el-input v-model="form.goodsCounts" placeholder="未填入商品数量" readonly/>
        </el-form-item>
        <el-form-item label="商品规格" prop="stander">
          <el-input v-model="form.stander" placeholder="未填入商品规格" readonly/>
        </el-form-item>
        <el-form-item label="删除标识" prop="isDel">
          <template slot="label">
            <span>
              删除标识
              <el-tooltip class="item" effect="dark" content="0-未删除，1-已删除" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
            </span>
          </template>
          <el-input v-model="form.isDel" placeholder="未填入删除标识" readonly />
        </el-form-item>
        <!-- <el-form-item label="扩展字段1" prop="ext1">
          <el-input v-model="form.ext1" placeholder="请输入扩展字段1" />
        </el-form-item>
        <el-form-item label="扩展字段2" prop="ext2">
          <el-input v-model="form.ext2" placeholder="请输入扩展字段2" />
        </el-form-item>
        <el-form-item label="扩展字段3" prop="ext3">
          <el-input v-model="form.ext3" placeholder="请输入扩展字段3" />
        </el-form-item>
        <el-form-item label="扩展字段4" prop="ext4">
          <el-input v-model="form.ext4" placeholder="请输入扩展字段4" />
        </el-form-item>
        <el-form-item label="扩展字段5" prop="ext5">
          <el-input v-model="form.ext5" placeholder="请输入扩展字段5" />
        </el-form-item> -->
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listCart, getCart, delCart, addCart, updateCart } from "@/api/system/cart";
import { getGoods } from "@/api/system/goods";
//引入用户信息查询
import { getUserInfo } from "@/api/system/userInfo";

export default {
  name: "Cart",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 购物车管理表格数据
      cartList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      openEdit: false,
      // 是否显示弹出层
      openView: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        goodsId: null,
        goodsCounts: null,
        stander: null,
        isDel: null,
        ext1: null,
        ext2: null,
        ext3: null,
        ext4: null,
        ext5: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询购物车管理列表 */
    async getList() {
      this.loading = true;
      try {
        const response = await listCart(this.queryParams);
        this.cartList = response.rows;
        this.total = response.total;
        await this.updateRealName();
        await this.updateGoodsName();
      } catch (error) {
        console.error('获取地址列表失败:', error);
      } finally {
        this.loading = false;
      }
    },
    // 新增元素的商品名称
    async updateGoodsName() {
      for (let i = 0; i < this.cartList.length; i++) {
        // console.log(this.cartList[i].shopId);
        let goodsInfo = await getGoods(this.cartList[i].goodsId);
        this.$set(this.cartList[i], 'goodsName', goodsInfo.data.goodsName);
      }
    },
    // 新增元素真实姓名和昵称
    async updateRealName() {
      for (let i = 0; i < this.cartList.length; i++) {
        let userInfo = await getUserInfo(this.cartList[i].userId);
        this.$set(this.cartList[i], 'realName', userInfo.data.realName);
        this.$set(this.cartList[i], 'nickName', userInfo.data.nickName);
      }
    },
    handleClickGoods() {
      // 这里可以弹出确认框，或者直接跳转
      this.$confirm('确定将前往“商品管理”修改?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        // 用户点击确认后的逻辑，例如跳转
        this.goToGoods();
      }).catch(() => {
        // 用户点击取消后的逻辑（如果有需要）
      });
    },
    handleClickUser() {
      // 这里可以弹出确认框，或者直接跳转
      this.$confirm('确定将前往“用户信息管理”修改?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        // 用户点击确认后的逻辑，例如跳转
        this.goToUserInfo();
      }).catch(() => {
        // 用户点击取消后的逻辑（如果有需要）
      });
    },
    // 前往商店管理页面
    goToGoods() {
      console.log("跳转到商品管理页面");
      this.$router.push({ path: '/tanpuhui/goods' });
      this.openEdit = false;
    },
    // 前往用户信息管理页面
    goToUserInfo() {
      console.log("跳转到用户信息管理页面");
      this.$router.push({ path: '/tanpuhui/userInfo' });
      this.openEdit = false;
    },
    // 取消按钮
    cancel() {
      this.openEdit = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cartId: null,
        userId: null,
        goodsId: null,
        goodsCounts: null,
        stander: null,
        createTime: null,
        updateTime: null,
        isDel: null,
        ext1: null,
        ext2: null,
        ext3: null,
        ext4: null,
        ext5: null
      };
      this.resetForm("form");
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.openView = true;
      this.form = row;
      this.title = "购物车管理详细（如需修改，请通过修改按钮进行修改）";
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cartId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openEdit = true;
      this.title = "添加购物车管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cartId = row.cartId || this.ids
      getCart(cartId).then(response => {
        this.form = response.data;
        this.openEdit = true;
        this.title = "修改购物车管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cartId != null) {
            updateCart(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openEdit = false;
              this.getList();
            });
          } else {
            addCart(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openEdit = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cartIds = row.cartId || this.ids;
      this.$modal.confirm('是否确认删除购物车管理Id为"' + cartIds + '"的数据项？').then(function () {
        return delCart(cartIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/cart/export', {
        ...this.queryParams
      }, `cart_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
